/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Category;
import br.com.foxline.clinicplus.entity.Item;
import br.com.foxline.clinicplus.entity.Procedure;
import br.com.foxline.clinicplus.entity.TemplateAppraisal;
import br.com.foxline.clinicplus.enums.TypeItem;
import br.com.foxline.clinicplus.service.CategoryService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.service.TemplateAppraisalService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DualListModel;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerTemplateAppraisal implements Serializable {

    @EJB
    TemplateAppraisalService templateAppraisalService;
    @EJB
    ProcedureService procedureService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    CategoryService categoryService;
    private TemplateAppraisal templateAppraisal;
    private DualListModel<Procedure> listModel;
    private Category category;
    private Item item;
    private List<Item> items;
    private List<Category> categorys;

    @PostConstruct
    public void init() {
        templateAppraisal = new TemplateAppraisal();
        category = new Category();
        item = new Item();
        items = new ArrayList<Item>();
        categorys = new ArrayList<Category>();

        List<Procedure> source = procedureService.findAll();
        List<Procedure> target = new ArrayList<Procedure>();

        listModel = new DualListModel<Procedure>(source, target);
        
        proxOrdemCategoria();
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public TemplateAppraisal getTemplateAppraisal() {
        return templateAppraisal;
    }

    public void setTemplateAppraisal(TemplateAppraisal templateAppraisal) {
        this.templateAppraisal = templateAppraisal;
    }

    public DualListModel<Procedure> getListModel() {
        return listModel;
    }

    public void setListModel(DualListModel<Procedure> listModel) {
        this.listModel = listModel;
    }

    public void addItemCategory() {
        if (item.getTitulo() == null || item.getTitulo().isEmpty()) {
            Msg.messagemError("Campo 'Titulo' é obrigatório !");
            return;
        }
        items.add(item);
        item.setCategory(category);
        item = new Item();
        proxOrdemItem();
    }

    public void addCategory() {

        if (category.getTitulo() == null || category.getTitulo().isEmpty()) {
            Msg.messagemError("Campo 'Nome' é obrigatório!");
            return;
        }
        
        category.setItems(items);
        category.setStatus(true);
        categorys.add(category);
        category = new Category();
        items = new ArrayList<Item>();
    }

    public void save() {
        try {

            templateAppraisal.setCreateAt(new Date());
            templateAppraisal.setSystemUser(systemUserService.getCurrentUser());
            templateAppraisal.setCategorys(categorys);
            templateAppraisal.setProcedures(listModel.getTarget());
            templateAppraisalService.save(templateAppraisal);
            Msg.messagemInfo("Operação realizada com Sucesso !");
            init();
        } catch (Exception e) {
            Msg.messagemInfo("Erro na Operação!");
        }

    }

    public List<SelectItem> selectTypeItem() {
        List<SelectItem> paymentTypes = new ArrayList<SelectItem>();
        for (TypeItem ms : TypeItem.values()) {
            paymentTypes.add(new SelectItem(ms, ms.getDesc()));
        }
        return paymentTypes;
    }

    public void removerItem(int id) {
        items.remove(id);
    }

    public void removerCategory(int id) {
        categorys.remove(id);
    }

    public void proxOrdemCategoria() {
        
        if (categorys != null && !categorys.isEmpty()) {
            category.setOrdem(categorys.size() + 1);
        }else{
            category.setOrdem(1);
        }
        
        proxOrdemItem();
        
    }
    public void proxOrdemItem() {
        
        if (items != null && !items.isEmpty()) {
            item.setOrdem(items.size() + 1);
        }else{
            item.setOrdem(1);
        }
        
        
    }
}
